import { Component, OnInit,Output,Input,EventEmitter,ViewChild } from '@angular/core';
const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
import { HttpClient, HttpParams, HttpHeaders } from '@angular/common/http';
import { Router } from '@angular/router';
import { NzModalRef, NzFormatEmitEvent, NzModalService, NzTreeComponent, NzTreeNodeOptions, NzMessageService } from 'ng-zorro-antd';
import { PagesService } from '../../services/pages.service';
import { UtilService } from '../../services/util.service';
import { RequestService } from '../../services/request.service';
declare var layui: any;
declare var layer: any;
declare var $: any;

@Component({
  selector: 'app-tree2',
  templateUrl: './tree2.component.html',
  styleUrls: ['./tree2.component.scss']
})
export class Tree2Component implements OnInit {

  constructor(public router: Router, private http: HttpClient, public interfaceService: PagesService, public utilService: UtilService,public request:RequestService) {
    
  }

  @Output()  private  outer=new EventEmitter();
  @Input() puc_msg:any; 

  
  unitHier_list;
  unitStatus_list;

  ngOnInit() {
    this.xiala();
    this.mkjiazai();
  }

  mkjiazai(){
    /* layui.config({
      base: '../../../../../assets/layui/layui_exts/'
    }).extend({
      dynamicCondition: 'dynamicCondition/dynamicCondition',// 高级查询
      transfer: 'transfer2/transfer',// 穿梭
      dtree: 'dtree/dtree'// dtree树
    }).use(['table','form','transfer','dtree']); //加载入口 */
  }
  
  xiala(){
    this.interfaceService.getPage("/erp/user/hierDtl/pulldownwarehfilter?unitType=WH").subscribe((data0: any) => {//手动下拉
      if (this.interfaceService.codeValidation(data0)) {
      if (data0.code == 200) {
        var sele = data0.object;
        this.unitHier_list = sele.unitHier;//	组织层级下拉
        this.unitStatus_list = sele.unitStatus;//组织状态下拉
      }
    }
    });
  }

  nodeid=""
  popup01_que(){
    this.nodeid=""
    /* layui.use(['table', 'dtree'], (obj)=>{
      var dtree = layui.dtree,table=layui.table;
      var params = dtree.getCheckbarNodesParam("toolbarTree1");
      // console.log(JSON.stringify(params));·
      for(var i=0;i<params.length;i++){
        this.nodeid=this.nodeid+params[i]["nodeId"]+","
      }
      this.nodeid=this.nodeid.substring(0,this.nodeid.length-1);//截取下标0开始到倒数第二位
      this.popup01_hide();
      this.outer.emit(this.nodeid)    
    }); */
    layui.use(['table', 'dtree'], (obj)=>{
      var dtree = layui.dtree,table=layui.table;
      var params = dtree.getCheckbarNodesParam("toolbarTree1");
      console.log(params);
      for(var i=0;i<params.length;i++){
        if(params[i]["leaf"]){
          this.nodeid=this.nodeid+params[i]["nodeId"]+","
        }
      }
      this.nodeid=this.nodeid.substring(0,this.nodeid.length-1);//截取下标0开始到倒数第二位
      console.log(this.nodeid)
      this.popup01_hide();
      this.outer.emit(this.nodeid)    
    });
    this.popup01_hide(); 
  }

  popup01_url='';
  popup01_switchUnitId='';
  popup01_para
  popup01_check=true
  popup01 = false;
  /**
   * 树弹窗选择，默认多选
   * @param url 
   * @param para  第二参数可选，为传递参数数据
   * @param radio 第三参数可选，选择代表单选
   */
  popup01_show(url,para?,radio?): void {
    this.popup01_url=url
    this.popup01_para=para;
    if (typeof(radio) == "undefined"){
      this.popup01_check=true
    }else{
      this.popup01_check=false
    }
    this.popup01 = true;
	  setTimeout(() => {
      this.popup01_table();
    }, this.utilService.setTime1);
  }
  popup01_hide(): void {
    this.popup01 = false;
    this.popup01_url=null;
    this.popup01_ss1=null;
  }

  popup01_data={}
  cengji='';
  zhuangt='';
  popup01_ss1=null;
  popup01_ss2=null;
  popup01_table(){
    if (this.popup01_ss1 == null) { this.cengji = "" } else { this.cengji = this.popup01_ss1 }
    if (this.popup01_ss2 == null) { this.zhuangt = "" } else { this.zhuangt = this.popup01_ss2 }
    if(typeof(this.popup01_para) == "undefined"){
      this.popup01_data={}
      Object.assign(this.popup01_data,{unitHierId:this.cengji,unitStatus:this.zhuangt});
    }else{
      this.popup01_data={}
      Object.assign(this.popup01_data, this.popup01_para,{unitHierId:this.cengji,unitStatus:this.zhuangt});
    }
    this.popup01_listTable(this.popup01_url);
  }

  popup01_listTable(url):void{
    layui.use(['table', 'dtree'], (obj)=>{
      var dtree = layui.dtree,  $ = layui.jquery;
      
      var DTree1 = dtree.render({

        elem: "#toolbarTree1",
        url: url,
        request:this.popup01_data,
        dataStyle:"layuiStyle",
        checkbar: this.popup01_check, 
        checkbarType: "no-all", // 默认就是all上下级联，其他的值为： no-all半选  p-casc单向级联   self独立  only仅存一个
        response:{
          statusName: "code", //返回标识（必填）
          statusCode: 200, //返回码（必填）
          message: "message", //返回信息（必填）
          rootName: "object", //根节点名称（必填）
          treeId: "treeId", //节点ID（必填）
          parentId: "parentId", //父节点ID（必填）
          title: "treeName", //节点名称（必填）
          childName: "treeVOs",//子节点名称
          last: "last", //是否最后一级节点（true：是，false：否，布尔值）
          level: "level",//级别
          spread: "spread", //节点展开状态
          basicData: "param", //表示用户自定义需要存储在树节点中的数据
        }

        
      });


      /**
       * 节点双击
       */
      dtree.on("nodedblclick('toolbarTree1')" ,(obj)=>{
        if(this.popup01_check==false){//单选
          /* console.log(obj.param); // 点击当前节点传递的参数
          console.log(obj.dom); // 当前节点的jquery对象
          console.log(obj.childParams); // 当前节点的所有子节点参数
          console.log(obj.parentParam); // 当前节点的父节点参数 */
          this.popup01_hide();
          this.outer.emit(obj.param)
        }
        
      });


    });
  }


}
